﻿#include "qbullet.h"
#include<QDebug>
QBullet::QBullet(const QString &fileName,QGraphicsScene *scene,qreal angle):QPixItem(fileName,scene)
{
    this->scene=scene;
    qreal dx,dy;
    qreal rad;
    rad=angle*3.14/180;
    dx=88*cos(rad);
    dy=88*sin(rad);
    this->setPos(scene->width()/2+dx,scene->height()-dy);
    this->setRotation(90-angle);

}

void QBullet::advance(int phase)
{
    if(mapToScene(0,0).x()<=0||mapToScene(0,0).x()>=1295||mapToScene(0,0).y()<=0){
        delete this;
    }
    else{
        //场景中的y坐标每次减20
        this->setPos(mapToScene(0,-20));
    }
}

void QBullet::doColliding()
{
    QNet *net=new QNet(":/image/net2.png",this->scene);
    net->setPos(mapToScene(0,0));
    //子弹消失
    delete this;
}

void QBullet::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawPixmap(-pix.width()/2,-pix.height(),pix);
    //将碰到场景中的任意东西加入到链表中
    if(this->collidingItems().count()>0){
        doColliding();
    }
}
